package com.cooba.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cooba.R;
import com.cooba.activitys.BannerDetailsActivity;
import com.cooba.application.MyApplication;
import com.cooba.bean.BannerBean;
import com.cooba.bean.BannerStatistic;
import com.cooba.views.BannerLayout;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/20.
 */
public class BannerUtils {
    private static List<BannerStatistic.BannerInfo> bannerInfoList = new ArrayList<>();

    public static View getCustomHeaderView(final Context context, final List<BannerBean.BannerListEntity> bannerlist) {
        View headerView = View.inflate(context, R.layout.view_custom_header, null);
        final BannerLayout bannerLayout = (BannerLayout) headerView.findViewById(R.id.banner);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        int height = (int) (width * 0.56);
        bannerLayout.setLayoutParams(new LinearLayout.LayoutParams(width, height));
        final List<String> urls = new ArrayList<>();
        for (int i = 0; i < bannerlist.size(); i++) {
            if (MyApplication.getMyApplication().getIsInCar()) {
                urls.add("http://a.cooba.cc/" + bannerlist.get(i).getHome_pic());
            } else {
                urls.add(bannerlist.get(i).getHome_pic());
            }
        }
        bannerLayout.setViewUrls(urls);

        getBannerStatistic(bannerlist.get(0), "1",context);
        //添加监听事件
        bannerLayout.setOnBannerStatisticListener(new BannerLayout.OnBannerStatisticListener() {
            @Override
            public void onBannerStatistic(int position) {
                //切换一次 拼装一次广告数据
                if (position < bannerlist.size()) {
                    getBannerStatistic(bannerlist.get(position), "1",context);
                }

            }
        });
        bannerLayout.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, BannerDetailsActivity.class);
                intent.putExtra("bannerurl", bannerlist.get(position).getHref());
               context.startActivity(intent);
                getBannerStatistic(bannerlist.get(position), "0",context);
            }
        });
        return headerView;
    }

    //获取广告统计信息
    public static void getBannerStatistic(BannerBean.BannerListEntity bannerEntity, String eventtype,Context context) {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String happenTime = sDateFormat.format(new java.util.Date());
        String mac = CommonUtil.getMacAddress(context);
        String ip = CommonUtil.getLocalIpAddress(context);
        String xLine = CommonUtil.getLocation(context, 0);//经度
        String yLine = CommonUtil.getLocation(context, 1);//纬度
        String clientOs = "Android";
        String clientType = android.os.Build.VERSION.RELEASE;
        String login_id = SharedPreferencesUtil.getData(context, "login_id", "");
        String username = "";
        String netType;
        if (MyApplication.getMyApplication().getIsInCar()) {
            netType = "0";
        } else {
            netType = "1";
        }
        String platform = "0";

        String bannerId = bannerEntity.getId();
        String name = bannerEntity.getName();
        String path = bannerEntity.getPath();
        String payType = bannerEntity.getPay_type();
        String eventType = eventtype;

        BannerStatistic.BannerInfo bannerInfo = new BannerStatistic.BannerInfo();
        bannerInfo.setMac(mac);
        bannerInfo.setIp(ip);
        bannerInfo.setClientOs(clientOs);
        bannerInfo.setClientType(clientType);
        bannerInfo.setLoginId(login_id);
        bannerInfo.setUserName(username);
        bannerInfo.setNetType(netType);
        bannerInfo.setxLine(xLine);
        bannerInfo.setyLine(yLine);
        bannerInfo.setHappenTime(happenTime);
        bannerInfo.setPlatform(platform);
        bannerInfo.setBannerId(bannerId);
        bannerInfo.setName(name);
        bannerInfo.setPath(path);
        bannerInfo.setPayType(payType);
        bannerInfo.setEventType(eventType);
        bannerInfoList.add(bannerInfo);

        Log.d("wei", "Json数据1:" + new Gson().toJson(bannerInfo));
        CommonUtil.writeTxtToFile(new Gson().toJson(bannerInfo));

        BannerStatistic bannerStatistic = new BannerStatistic();
        bannerStatistic.setStatisticsList(bannerInfoList);
        Log.d("wei", "Json数据2:" + new Gson().toJson(bannerStatistic));
    }
}
